import os
from time import sleep
from bs4 import BeautifulSoup
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from awtools.log import log
from selenium import webdriver
import re

"""

"""

class saveHtml(object):

    phone = '15810898447'
    pasword = '123456'

    def __new__(cls, url):
        log('这里是开始运行  new ')
        o = object.__new__(cls)
        return o

    def __init__(self, url, phone = None, pasword = None):
        if  phone != None:
            self.phone = phone
        if  pasword != None:
            self.pasword = pasword

        self.driver = webdriver.Chrome()
        self.driver.get(url=url)
        self.driver.maximize_window()
        self.base = ActionChains(self.driver)

        try:
            self.login()
            self.chooseSubjects()
            self.closes()
        except Exception as e:
            print(e)



    def login(self):
        WebDriverWait(driver=self.driver, timeout=20, poll_frequency=1).until(lambda driver: driver.find_element_by_id('login'))
        self.driver.find_element_by_id('txtName').send_keys(self.phone)
        self.driver.find_element_by_id('pswPassword').send_keys(self.pasword)
        self.driver.find_element_by_id('login').click()

    def chooseSubjects(self):
        btn = WebDriverWait(driver=self.driver, timeout=20, poll_frequency=1).until(lambda driver: driver.find_element_by_id('topSelectedSoft'))
        btn.click()
        self.sleep1()
        self.driver.find_element_by_class_name('qiehuan').click()
        self.sleep1()
        liObjs = self.driver.find_element_by_css_selector('.category > li')
        # 一级分类
        for item in liObjs:
            log.debug('进入循环')
            self.oneTitle = item.text
            item.click()
            self.sleep1()
            subNs = self.driver.find_element_by_css_selector('.kemuList_lef > li')
            #二级分类
            for sub in subNs:
                log.debug('进入2循环')
                self.twoTitle = sub.text
                sub.click()
                self.sleep1()
                menus = self.driver.find_element_by_css_selector('.kemuList_rig > li')
                for m in menus:
                    self.threeTitle = m.text
                    m.click()
                    log.debug('进入3循环')
                    self.sleep1()
                    btn = WebDriverWait(driver=self.driver, timeout=20, poll_frequency=1).until(lambda driver: driver.find_element_by_class_name('rout-mfst'))
                    iszhezhao = self.isElement('.close_btn')
                    if iszhezhao:
                        iszhezhao.click()
                        self.sleep1()
                        log.debug('关闭遮罩')
                    btn.click()
                    log.debug('点击免费')
                    self.sleep1()
                    self.save()
                    return False

    def isElement(self, cssSelect):
        try:
            btn = self.driver.find_element_by_css_selector(cssSelect)
            return btn
        except Exception as e:
            return False


    def save(self):
        log.debug('开始保存数据')
        tiNumObjs = self.driver.find_element_by_css_selector('.answerCard>ul>li')
        for k in range(len(tiNumObjs)):
            log.debug('第'+str(k+1)+'道题')
            tiNumObjs[k].click()
            html = self.driver.page_source
            html = self.getMainHtml(html)

            fileName = './html/'+self.oneTitle+'/'+self.twoTitle+'/'+self.threeTitle+'/'
            if not os.path.exists(fileName):
                os.makedirs(fileName)
            fileName = fileName+str(k)+'.html'
            log.file_put_content(file=fileName, msg=html)
        return True


    def getMainHtml(self, html):
        bObj = BeautifulSoup(html, features='html.parser')
        aObj = bObj.find('div', {'class':'conleft'})
        return aObj.__str__()

    def filterHtml(self, html):
        #先过滤CDATA
        re_script=re.compile('<s*script[^>]*>[^<]*<s*/s*scripts*>',re.I)#Script
        re_style=re.compile('<s*style[^>]*>[^<]*<s*/s*styles*>',re.I)#style
        re_img=re.compile('<img*(.*?)>',re.I)#style
        re_br=re.compile('<brs*?/?>')#处理换行
        re_comment=re.compile('<!--[^>]*-->')#HTML注释
        s=re_script.sub('', html) #去掉SCRIPT
        s=re_style.sub('',s)#去掉style
        s=re_img.sub('',s)#去掉style
        s=re_br.sub('n',s)#将br转换为换行
        s=re_comment.sub('',s)#去掉HTML注释
        #去掉多余的空行
        blank_line=re.compile('n+')
        s=blank_line.sub('n',s)
        return s


    def sleep1(self):
        sleep(0.5);

    def closes(self):
        log.debug('正在关闭浏览器')
        sleep(3)
        # self.driver.close()
        self.driver.quit()















